package cn.edu.zafu.bigdata.x.zzzznnn.j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserRegistrationLogin {
    public static boolean registerUser(String username, String password) {
        try {
            Connection connection = DBConnection.getConnection();
            String sql = "INSERT INTO users (username, password) VALUES (?,?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            int rowsInserted = statement.executeUpdate();
            statement.close();
            connection.close();
            return rowsInserted > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean loginUser(String username, String password) {
        try {
            DBConnection connection = (DBConnection) DBConnection.getConnection();
            String sql = "SELECT * FROM users WHERE username =? AND password =?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();
            boolean isUserExist = resultSet.next();
            resultSet.close();
            statement.close();
            connection.close();
            return isUserExist;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
